package cn.iocoder.yudao.module.product.controller.admin.shopfavorite;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.product.controller.admin.shopfavorite.vo.ShopFavoritePageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.shopfavorite.vo.ShopFavoriteRespVO;
import cn.iocoder.yudao.module.product.controller.admin.shopfavorite.vo.ShopFavoriteSaveReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.shopfavorite.ShopFavoriteDO;
import cn.iocoder.yudao.module.product.service.shopfavorite.ShopFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 商铺收藏")
@RestController
@RequestMapping("/product/shop-favorite")
@Validated
public class ShopFavoriteController {

    @Resource
    private ShopFavoriteService shopFavoriteService;

    @PostMapping("/create")
    @Operation(summary = "创建商铺收藏")
    @PreAuthorize("@ss.hasPermission('product:shop-favorite:create')")
    public CommonResult<Long> createShopFavorite(@Valid @RequestBody ShopFavoriteSaveReqVO createReqVO) {
        return success(shopFavoriteService.createShopFavorite(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商铺收藏")
    @PreAuthorize("@ss.hasPermission('product:shop-favorite:update')")
    public CommonResult<Boolean> updateShopFavorite(@Valid @RequestBody ShopFavoriteSaveReqVO updateReqVO) {
        shopFavoriteService.updateShopFavorite(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商铺收藏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:shop-favorite:delete')")
    public CommonResult<Boolean> deleteShopFavorite(@RequestParam("id") Long id) {
        shopFavoriteService.deleteShopFavorite(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商铺收藏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:shop-favorite:query')")
    public CommonResult<ShopFavoriteRespVO> getShopFavorite(@RequestParam("id") Long id) {
        ShopFavoriteDO shopFavorite = shopFavoriteService.getShopFavorite(id);
        return success(BeanUtils.toBean(shopFavorite, ShopFavoriteRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商铺收藏分页")
    @PreAuthorize("@ss.hasPermission('product:shop-favorite:query')")
    public CommonResult<PageResult<ShopFavoriteRespVO>> getShopFavoritePage(@Valid ShopFavoritePageReqVO pageReqVO) {
        PageResult<ShopFavoriteDO> pageResult = shopFavoriteService.getShopFavoritePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShopFavoriteRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商铺收藏 Excel")
    @PreAuthorize("@ss.hasPermission('product:shop-favorite:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShopFavoriteExcel(@Valid ShopFavoritePageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopFavoriteDO> list = shopFavoriteService.getShopFavoritePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商铺收藏.xls", "数据", ShopFavoriteRespVO.class,
                BeanUtils.toBean(list, ShopFavoriteRespVO.class));
    }

}